package com.whnk.attendance.controller;

import com.whnk.attendance.service.IPmSignInfoService;
import com.whnk.attendance.vo.PmSignListImportVo;
import com.whnk.attendance.vo.PmSignListVo;
import com.whnk.attendance.vo.PmSignQueryVo;
import com.whnk.common.core.utils.DateUtils;
import com.whnk.common.core.utils.SecurityUtils;
import com.whnk.common.core.utils.poi.ExcelUtil;
import com.whnk.common.core.web.controller.BaseController;
import com.whnk.common.core.web.domain.AjaxResult;
import com.whnk.common.core.web.page.TableDataInfo;
import com.whnk.common.log.annotation.Log;
import com.whnk.common.log.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * 项目考勤信息Controller
 *
 * @author whnk
 * @date 2021-11-17
 */
@Api(tags = "项目考勤信息Controller")
@RestController
@RequestMapping("/pmSignInfo")
public class PmSignInfoController extends BaseController
{
    @Autowired
    private IPmSignInfoService pmSignInfoService;

    private static Set<String> Valid_Excel_Type = new HashSet<>();

    static {
        Valid_Excel_Type.add(".xls");
        Valid_Excel_Type.add(".xlsx");
    }


    /**
     * 查询项目考勤信息列表
     */
    @ApiOperation(value = "分页查询项目考勤信息列表")
    //@PreAuthorize(hasPermi = "attendance:info:list")
    @GetMapping("/list")
    public TableDataInfo list(PmSignQueryVo pmSignQueryVo)
    {
        startPage();
        List<PmSignListVo> list = pmSignInfoService.selectPmSignInfoList(pmSignQueryVo);
        return getDataTable(list);
    }

    /**
     * 导出项目考勤信息列表
     */
    @ApiOperation(value = "导出项目考勤信息列表")
    //@PreAuthorize(hasPermi = "attendance:info:export")
    @Log(title = "项目考勤信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PmSignQueryVo pmSignQueryVo) throws IOException
    {
        List<PmSignListVo> pmSignListVos = pmSignInfoService.selectPmSignInfoList(pmSignQueryVo);
        AtomicInteger index = new AtomicInteger(1);
        List<PmSignListImportVo> list = pmSignListVos.stream().map(pmSignListVo -> {
            PmSignListImportVo pmSignListImportVo = new PmSignListImportVo();
            BeanUtils.copyProperties(pmSignListVo, pmSignListImportVo);
            pmSignListImportVo.setExcelNum(index.getAndIncrement()+"");
            return pmSignListImportVo;
        }).collect(Collectors.toList());

        ExcelUtil<PmSignListImportVo> util = new ExcelUtil<PmSignListImportVo>(PmSignListImportVo.class);
        util.exportExcel(response, list, "项目考勤");
    }


    /**
     * 导入项目考勤信息列表
     */
    @ApiOperation(value = "导入项目考勤信息列表")
    @PostMapping("/import")
    public AjaxResult imports(@RequestPart("excelFile") MultipartFile file) throws Exception {
        ExcelUtil<PmSignListImportVo> util = new ExcelUtil<PmSignListImportVo>(PmSignListImportVo.class);
        Date signDt = null;
        String fileName = file.getOriginalFilename();
        int fileIndex = fileName.lastIndexOf(".");
        String filetype = fileName.substring(fileIndex);
        if(!Valid_Excel_Type.contains(filetype)){
            return AjaxResult.error("文件类型不正确,必须是xls,xlsx");
        }
        String filePrefix = fileName.substring(0,fileIndex);
        //导入的文件格式,以yyyy_MM结尾,举例:考勤_2021_11.xls
        try{
            String dateString = filePrefix.substring(filePrefix.length()-7);
            signDt = DateUtils.dateTime("yyyy_MM", dateString);
        }catch(Exception ex){
            logger.error(ex.getMessage(),ex);
            return AjaxResult.error("导入的文件格式,必须yyyy_MM结尾");
        }
        List<PmSignListImportVo> list = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = pmSignInfoService.importPmSignInfo(list, signDt, operName);
        return AjaxResult.success(message);
    }
    /**
     * 获取项目考勤信息详细信息
     */
    /*@ApiOperation(value = "根据id获取项目考勤信息详细信息")
    //@PreAuthorize(hasPermi = "attendance:info:query")
    @GetMapping(value = "/{signId}")
    public AjaxResult getInfo(@PathVariable("signId") Long signId)
    {
        return AjaxResult.success(pmSignInfoService.selectPmSignInfoBySignId(signId));
    }*/

   /* *
     * 新增项目考勤信息
     */
  /*  @ApiOperation(value = "新增项目考勤信息")
   // @PreAuthorize(hasPermi = "attendance:info:add")
    @Log(title = "项目考勤信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PmSignInfo pmSignInfo)
    {
        return toAjax(pmSignInfoService.insertPmSignInfo(pmSignInfo));
    }*/

     /**
     * 修改项目考勤信息
     */
   /* @ApiOperation(value = "修改项目考勤信息")
    //@PreAuthorize(hasPermi = "attendance:info:edit")
    @Log(title = "项目考勤信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PmSignInfo pmSignInfo)
    {
        return toAjax(pmSignInfoService.updatePmSignInfo(pmSignInfo));
    }*/


    /**
     * 删除项目考勤信息
     * @param signIds
     * @return
     */
   /* @ApiOperation(value = "根据signIds删除项目考勤信息")
    //@PreAuthorize(hasPermi = "attendance:info:remove")
    @Log(title = "项目考勤信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{signIds}")
    public AjaxResult remove(@PathVariable Long[] signIds)
    {
        return toAjax(pmSignInfoService.deletePmSignInfoBySignIds(signIds));
    }*/
}
